<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

  if (tep_not_null($action)) {
    switch ($action) {
      case 'insert':
        $reseller_city_id = tep_db_prepare_input($HTTP_POST_VARS['reseller_city_id']);
        $reseller_name    = tep_db_prepare_input($HTTP_POST_VARS['reseller_name']);
        $reseller_fone    = tep_db_prepare_input($HTTP_POST_VARS['reseller_fone']);
        $reseller_mail    = tep_db_prepare_input($HTTP_POST_VARS['reseller_mail']);
        $reseller_cnpj    = tep_db_prepare_input($HTTP_POST_VARS['reseller_cnpj']);
        if (isset($HTTP_POST_VARS['reseller_install'])) {
          $reseller_install = tep_db_prepare_input($HTTP_POST_VARS['reseller_install']);
        } else {
          $reseller_install = "R";
        }
        
        tep_db_query("insert into " . TABLE_RESELLERS . " ( reseller_city_id, reseller_name, reseller_fone, reseller_mail, reseller_cnpj,reseller_install) values ('" . tep_db_input($reseller_city_id) . "', '" . tep_db_input($reseller_name) . "', '" . tep_db_input($reseller_fone) . "', '" . tep_db_input($reseller_mail) . "', '" . tep_db_input($reseller_cnpj) . "','" . tep_db_input($reseller_install) . "')");

        tep_redirect(tep_href_link(FILENAME_RESELLERS));
        break;
      case 'save':
        $reseller_id = tep_db_prepare_input($HTTP_GET_VARS['cID']);
        $reseller_city_id = tep_db_prepare_input($HTTP_POST_VARS['reseller_city_id']);
        $reseller_name = tep_db_prepare_input($HTTP_POST_VARS['reseller_name']);
        $reseller_fone = tep_db_prepare_input($HTTP_POST_VARS['reseller_fone']);
        $reseller_mail = tep_db_prepare_input($HTTP_POST_VARS['reseller_mail']);
        $reseller_cnpj = tep_db_prepare_input($HTTP_POST_VARS['reseller_cnpj']);
        if (isset($HTTP_POST_VARS['reseller_install'])) {
          $reseller_install = tep_db_prepare_input($HTTP_POST_VARS['reseller_install']);
        } else {
          $reseller_install = "R";
        }
        
        tep_db_query("update " . TABLE_RESELLERS . " set reseller_city_id = '" . tep_db_input($reseller_city_id) . "', reseller_name = '" . tep_db_input($reseller_name) . "', reseller_fone = '" . tep_db_input($reseller_fone) . "', reseller_mail = '" . tep_db_input($reseller_mail) . "', reseller_cnpj = '" . tep_db_input($reseller_cnpj) . "', reseller_install = '". tep_db_input($reseller_install) . "' where reseller_id = '" . (int)$reseller_id . "'");

        tep_redirect(tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $reseller_id));
        break;
      case 'deleteconfirm':
        $reseller_id = tep_db_prepare_input($HTTP_GET_VARS['cID']);

        tep_db_query("delete from " . TABLE_RESELLERS . " where reseller_id = '" . (int)$reseller_id . "'");

        tep_redirect(tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page']));
        break;
    }
  }

  require(DIR_WS_INCLUDES . 'template_top.php');
?>

    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_RESELLER_NAME; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_RESELLER_CNPJ; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_RESELLER_FONE; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_RESELLER_INSTALL; ?></td>                
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_RESELLER_CITY; ?></td>
                <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_RESELLER_MAIL; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
<?php
  $reseller_query_raw = "select a.reseller_id, a.reseller_name, a.reseller_cnpj, a.reseller_fone, a.reseller_install, b.city_name, a.reseller_mail from " . TABLE_CITIES . " b, " . TABLE_RESELLERS . " a where a.reseller_city_id = b.city_id order by b.city_name, a.reseller_name";
  $reseller_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $reseller_query_raw, $reseller_query_numrows);
  $reseller_query = tep_db_query($reseller_query_raw);
  while ($reseller = tep_db_fetch_array($reseller_query)) {
    if ((!isset($HTTP_GET_VARS['cID']) || (isset($HTTP_GET_VARS['cID']) && ($HTTP_GET_VARS['cID'] == $reseller['reseller_id']))) && !isset($cInfo) && (substr($action, 0, 3) != 'new')) {
      $cInfo = new objectInfo($reseller);
    }

    if (isset($cInfo) && is_object($cInfo) && ($reseller['reseller_id'] == $cInfo->reseller_id)) {
      echo '              <tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->reseller_id . '&action=edit') . '\'">' . "\n";
    } else {
      echo '              <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $reseller['reseller_id']) . '\'">' . "\n";
    }
?>            
                <td class="dataTableContent"><?php echo $reseller['reseller_name']; ?></td>
                <td class="dataTableContent"><?php echo $reseller['reseller_cnpj']; ?></td>
                <td class="dataTableContent"><?php echo $reseller['reseller_fone']; ?></td>
                <td class="dataTableContent"><?php echo $reseller['reseller_install']; ?></td>
                <td class="dataTableContent"><?php echo $reseller['city_name']; ?></td>
                <td class="dataTableContent" align="center"><?php echo $reseller['reseller_mail']; ?></td>
                <td class="dataTableContent" align="right"><?php if (isset($cInfo) && is_object($cInfo) && ($reseller['reseller_id'] == $cInfo->reseller_id) ) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $reseller['reseller_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>
              </tr>
<?php
  }
?>
              <tr>
                <td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="smallText" valign="top"><?php echo $reseller_split->display_count($reseller_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_RESELLERS); ?></td>
                    <td class="smallText" align="right"><?php echo $reseller_split->display_links($reseller_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page']); ?></td>
                  </tr>
<?php
  if (empty($action)) {
?>
                  <tr>
                    <td class="smallText" colspan="2" align="right"><?php echo tep_draw_button(IMAGE_NEW_RESELLER, 'plus', tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&action=new')); ?></td>
                  </tr>
<?php
  }
?>
                </table></td>
              </tr>
            </table></td>
<?php
  $heading = array();
  $contents = array();

  switch ($action) {
    case 'new':
      $heading[] = array('text' => '<strong>' . TEXT_INFO_HEADING_NEW_RESELLER . '</strong>');

      $contents = array('form' => tep_draw_form('resellers', FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&action=insert'));
      $contents[] = array('text' => TEXT_INFO_INSERT_INTRO);
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_NAME . '<br />' . tep_draw_input_field('reseller_name'));
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_CNPJ . '<br />' . tep_draw_input_field('reseller_cnpj'));
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_FONE . '<br />' . tep_draw_input_field('reseller_fone'));
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_INSTALL . '<br />' . tep_draw_radio_field('reseller_install', 'R') . '&nbsp;&nbsp;' . INSTALL_RESELLER . '&nbsp;<br>' . tep_draw_radio_field('reseller_install', 'M') . '&nbsp;&nbsp;' . INSTALL_HOME . '&nbsp;' );
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_MAIL . '<br />' . tep_draw_input_field('reseller_mail'));
      $contents[] = array('text' => '<br />' . TEXT_INFO_CITY_NAME . '<br />' .tep_draw_pull_down_menu('reseller_city_id', tep_get_cities()));
      $contents[] = array('align' => 'center', 'text' => '<br />' . tep_draw_button(IMAGE_SAVE, 'disk', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'])));
      break;
    case 'edit':
      $heading[] = array('text' => '<strong>' . TEXT_INFO_HEADING_EDIT_RESELLER . '</strong>');

      $contents = array('form' => tep_draw_form('resellers', FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->reseller_id . '&action=save'));
      $contents[] = array('text' => TEXT_INFO_EDIT_INTRO);
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_NAME . '<br />' . tep_draw_input_field('reseller_name', $cInfo->reseller_name));
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_CNPJ . '<br />' . tep_draw_input_field('reseller_cnpj', $cInfo->reseller_cnpj));
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_FONE . '<br />' . tep_draw_input_field('reseller_fone', $cInfo->reseller_fone));
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_INSTALL . '<br />' . tep_draw_radio_field('reseller_install', "R") . '&nbsp;&nbsp;' . INSTALL_RESELLER . '&nbsp;<br>' . tep_draw_radio_field('reseller_install', "M") . '&nbsp;&nbsp;' . INSTALL_HOME . '&nbsp;');
      $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_MAIL . '<br />' . tep_draw_input_field('reseller_mail', $cInfo->reseller_mail));
      $contents[] = array('text' => '<br />' . TEXT_INFO_CITY_NAME . '<br />' . tep_draw_pull_down_menu('reseller_city_id', tep_get_cities(), $cInfo->reseller_city_id));
      $contents[] = array('align' => 'center', 'text' => '<br />' . tep_draw_button(IMAGE_SAVE, 'disk', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->reseller_id)));
      break;
    case 'delete':
      $heading[] = array('text' => '<strong>' . TEXT_INFO_HEADING_DELETE_RESELLER . '</strong>');

      $contents = array('form' => tep_draw_form('resellers', FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->reseller_id . '&action=deleteconfirm'));
      $contents[] = array('text' => TEXT_INFO_DELETE_INTRO);
      $contents[] = array('text' => '<br /><strong>' . $cInfo->reseller_name . '</strong>');
      $contents[] = array('align' => 'center', 'text' => '<br />' . tep_draw_button(IMAGE_DELETE, 'trash', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->reseller_id)));
      break;
    default:
      if (isset($cInfo) && is_object($cInfo)) {
        $heading[] = array('text' => '<strong>' . $cInfo->reseller_name . '</strong>');

        $contents[] = array('align' => 'center', 'text' => tep_draw_button(IMAGE_EDIT, 'document', tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->reseller_id . '&action=edit')) . tep_draw_button(IMAGE_DELETE, 'trash', tep_href_link(FILENAME_RESELLERS, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->reseller_id . '&action=delete')));
// Andre, para debugar descomentar a linha debaixo para mostrar o id da revenda com o nome.
        $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_NAME . '<br />' . $cInfo->reseller_name . ' (' . $cInfo->reseller_id . ')');
//        $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_NAME . '<br />' . $cInfo->reseller_name);
        $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_CNPJ . ' ' . $cInfo->reseller_cnpj);
        $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_FONE . ' ' . $cInfo->reseller_fone);
        $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_INSTALL . ' ' . $cInfo->reseller_install);
        $contents[] = array('text' => '<br />' . TEXT_INFO_RESELLER_MAIL . ' ' . $cInfo->reseller_mail);
        $contents[] = array('text' => '<br />' . TEXT_INFO_CITY_NAME . ' ' . $cInfo->city_name);
      }
      break;
  }

  if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
    echo '            <td width="25%" valign="top">' . "\n";

    $box = new box;
    echo $box->infoBox($heading, $contents);

    echo '            </td>' . "\n";
  }
?>
          </tr>
        </table></td>
      </tr>
    </table>

<?php
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
